//
//  AnnounceViewModel.m
//  AppleMarket
//
//  Created by yangy on 31/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "AnnounceViewModel.h"

// model
#import "NewsModel.h"


@implementation AnnounceViewModel
- (instancetype)init
{
    self = [super init];
    if(self){
        [self.refershAnnounceCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                self.currentPage ++;
                NSDictionary *data = [x objectForKey:@"Data"];
                NSArray *dataArray = [data objectForKey:@"DataList"];
                NSArray *temp = [[dataArray.rac_sequence.signal map:^id _Nullable(id  _Nullable value) {
                    return [NewsModel yy_modelWithDictionary:value];
                }] toArray];
                
                [temp sortedArrayUsingComparator:^NSComparisonResult(NewsModel *obj1, NewsModel  *obj2) {
                    NSTimeInterval t1 = [UtilsCommon timeIntervalForDateFormat:@"yyyy-MM-dd HH:mm:ss" date:obj1.CreateTime];
                    NSTimeInterval t2 = [UtilsCommon timeIntervalForDateFormat:@"yyyy-MM-dd HH:mm:ss" date:obj2.CreateTime];
                    if(t2 > t1){
                        return NSOrderedAscending;
                    }else if(t2 < t1){
                        return NSOrderedDescending;
                    }else{
                        return NSOrderedSame;
                    }
                }];
                self.pageCount = [[data objectForKey:@"PageCount"] integerValue];
                self.totalCount = [[data objectForKey:@"TotalCount"] integerValue]; 
                self.refershDatas = [temp mutableCopy];
            }
            [self.refershEndSubject sendNext:nil];
        } error:^(NSError * _Nullable error) {
            [self.refershEndSubject sendNext:nil];
        }];
        
        [self.refershMoreAnnounceCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                self.currentPage ++;
                NSDictionary *data = [x objectForKey:@"Data"];
                NSArray *dataArray = [data objectForKey:@"DataList"];
                NSArray *temp = [[dataArray.rac_sequence.signal map:^id _Nullable(id  _Nullable value) {
                    return [NewsModel yy_modelWithDictionary:value];
                }] toArray];
                
                [temp sortedArrayUsingComparator:^NSComparisonResult(NewsModel *obj1, NewsModel  *obj2) {
                    NSTimeInterval t1 = [UtilsCommon timeIntervalForDateFormat:@"yyyy-MM-dd HH:mm:ss" date:obj1.CreateTime];
                    NSTimeInterval t2 = [UtilsCommon timeIntervalForDateFormat:@"yyyy-MM-dd HH:mm:ss" date:obj2.CreateTime];
                    if(t2 > t1){
                        return NSOrderedAscending;
                    }else if(t2 < t1){
                        return NSOrderedDescending;
                    }else{
                        return NSOrderedSame;
                    }
                }];
                self.pageCount = [[data objectForKey:@"PageCount"] integerValue];
                self.totalCount = [[data objectForKey:@"TotalCount"] integerValue];
                [self.refershDatas addObjectsFromArray:temp];
            }
            [self.refershEndSubject sendNext:nil];
        } error:^(NSError * _Nullable error) {
            [self.refershEndSubject sendNext:nil];
        }];
    }
    return self;
}

- (RACSubject *)refershEndSubject
{
    if(!_refershEndSubject){
        _refershEndSubject = [RACSubject subject];
    }
    return _refershEndSubject;
}

- (RACCommand *)refershAnnounceCommand
{
    if(!_refershAnnounceCommand){
        _refershAnnounceCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            self.currentPage = 1;
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [NetRequest request_GetNewsListWithPageIndex:self.currentPage typeId:1 completeHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _refershAnnounceCommand;
}

- (RACCommand *)refershMoreAnnounceCommand
{
    if(!_refershMoreAnnounceCommand){
        _refershMoreAnnounceCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                if(self.currentPage > self.pageCount){
                    [self.refershEndSubject sendNext:nil];
                }else{
                    [NetRequest request_GetNewsListWithPageIndex:self.currentPage typeId:1 completeHandler:^(id  _Nonnull responseObj) {
                        [subscriber sendNext:responseObj];
                        [subscriber sendCompleted];
                    } fail:^(NSError * _Nonnull error) {
                        [subscriber sendError:error];
                    }];
                }
                return nil;
            }];
        }];;
    }
    return _refershMoreAnnounceCommand;
}

- (RACSubject *)detailClickSubject
{
    if(!_detailClickSubject){
        _detailClickSubject = [RACSubject subject];
    }
    return _detailClickSubject;
}

@end
